//请求前缀
var baseURL = "https://oxtoken.io";

var vm = new Vue({
    el: '#app',
    data: {
        phone:''
        ,password:''
    },
    methods: {
        login: function () {
            if(!vm.phone){
                this.$dialog.toast({
                    mes: '手机号不能为空',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }else if(!/1[3|4|5|6|7|8|9]\d{9}/.test(vm.phone)){
                this.$dialog.toast({
                    mes: '手机号格式错误',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }else if(vm.phone.length != 11){
                this.$dialog.toast({
                    mes: '您输的不是手机号',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }

            if(!vm.password){
                this.$dialog.toast({
                    mes: '密码不能为空',
                    icon: 'error',
                    timeout: 1500
                });
                return;
            }

            var pt = this;
            pt.$dialog.loading.open('登录中...');

            $.ajax({
                type: "POST",
                url: baseURL + "/api/v1/login",
                data: JSON.stringify({username:vm.phone,password: vm.password}),
                contentType: "application/json",
                dataType:  "json",
                success: function(r){
                    pt.$dialog.loading.close();
                    pt.$dialog.toast({
                        mes: '登录成功',
                        icon: 'success',
                        timeout: 1500,
                        callback:function () {
                            var token = 'OX ' + r.key;
                            localStorage.setItem("token", token);
                            location.href = 'index.html';
                        }
                    });
                },
                error:function(e){
                    pt.$dialog.loading.close();
                    if(typeof e.responseJSON != 'undefined'){
                        pt.$dialog.toast({
                            mes: e.responseJSON.code,
                            icon: 'error',
                            timeout: 1500
                        });
                    }else {
                        pt.$dialog.toast({
                            mes: '登录异常',
                            icon: 'error',
                            timeout: 1500
                        });
                    }
                }
            });
        }
    }
});